package cn.com.eado.crm.m.repository

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import cn.com.eado.crm.m.AppExecutors
import cn.com.eado.crm.m.bean.PostCompany
import cn.com.eado.crm.m.db.Company
import cn.com.eado.crm.m.db.EnterpriseInfo
import cn.com.eado.crm.m.db.Institute
import cn.com.eado.crm.m.db.MyDatabase
import cn.com.eado.crm.m.net.MyRetrofit
import cn.com.eado.crm.m.net.Okhttp3Utils
import cn.com.eado.crm.model.NetworkBoundResource
import cn.com.eado.crm.model.Resource
import cn.com.eado.crm.model.net.ApiResponse

class InstituteRepository {

    fun loadInstitute(userId: Int, page: Int, rows: Int): LiveData<Resource<Institute>> {
        return object: NetworkBoundResource<Institute, Institute>(AppExecutors()){
            override fun shouldLoadFromDb(): Boolean = false

            override fun loadFromDb(): LiveData<Institute> = MediatorLiveData<Institute>()

            override fun createCall() = MediatorLiveData<ApiResponse<Institute>>()

            override fun saveCallResult(item: Institute) {}

            override fun shouldFetch(data: Institute?) = true

            override fun createCall2(): LiveData<ApiResponse<Institute>>? = MyRetrofit.getInstance().api.getInstitute(/*null,null,null,null,null,null,*/page, rows)
        }.asLiveData()
    }

    fun saveInstitute(userId: Int, t: PostCompany): LiveData<Resource<String>> {
        return object: NetworkBoundResource<String, String>(AppExecutors()){
            override fun shouldLoadFromDb(): Boolean = false

            override fun loadFromDb(): LiveData<String> = MediatorLiveData<String>()

            override fun createCall() = MediatorLiveData<ApiResponse<String>>()

            override fun saveCallResult(item: String) {}

            override fun shouldFetch(data: String?) = true

            override fun createCall2(): LiveData<ApiResponse<String>>? = MyRetrofit.getInstance().api.saveCompany(t)
        }.asLiveData()
    }

    fun searchInstitute(userId: Int, page: Int, rows: Int, companyName: String): LiveData<Resource<Institute>> {
        return object: NetworkBoundResource<Institute, Institute>(AppExecutors()){
            override fun shouldLoadFromDb(): Boolean = false

            override fun loadFromDb(): LiveData<Institute> = MediatorLiveData<Institute>()

            override fun createCall() = MediatorLiveData<ApiResponse<Institute>>()

            override fun saveCallResult(item: Institute) {}

            override fun shouldFetch(data: Institute?) = true

            override fun createCall2(): LiveData<ApiResponse<Institute>>? = MyRetrofit.getInstance().api.searchInstitute(page, rows/*, companyName*/)
        }.asLiveData()
    }

}